package com.wc.acwing.暴力.保险箱窃贼;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/8/2 12:57
 * @description https://www.acwing.com/problem/content/5821/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 15;
    static char[][] g = new char[N][];
    static int[][] g2 = new int[N][5];
    static int[] x = new int[N];
    static int[] y = new int[N];
    static int n;

    public static void main(String[] args) {
        int T = sc.nextInt();
        for (int st = 1; st <= T; st++) {
            n = sc.nextInt();
            for (int i = 1; i <= n; i++) {
                g[i] = (" " + sc.next()).toCharArray();
                for (int j = 1; j <= 4; j++) g2[i][j] = g[i][j] - '0';
                char[] s = sc.next().toCharArray();
                x[i] = s[0] - '0';
                y[i] = s[2] - '0';
            }
            int res = 0;
            int rA, rB, rC, rD;
            rA = rB = rC = rD = -1;

            for (int a = 0; a <= 9; a++) {
                for (int b = 0; b <= 9; b++) {
                    for (int c = 0; c <= 9; c++) {
                        for (int d = 0; d <= 9; d++) {
                            boolean hasFs = false;
                            for (int i = 1; i <= n; i++) {
                                int eq = 0, cw = 0;
                                if (a == g2[i][1]) eq++;
                                else if (a == g2[i][2] && b != g2[i][2] || a == g2[i][3] && c != g2[i][3] || a == g2[i][4] && d != g2[i][4])
                                    cw++;

                                if (b == g2[i][2]) eq++;
                                else if (b == g2[i][1] && a != g2[i][1] || b == g2[i][3] && c != g2[i][3] || b == g2[i][4] && d != g2[i][4])
                                    cw++;

                                if (c == g2[i][3]) eq++;
                                else if (c == g2[i][1] && a != g2[i][1] || c == g2[i][2] && b != g2[i][2] || c == g2[i][4] && d != g2[i][4])
                                    cw++;

                                if (d == g2[i][4]) eq++;
                                else if (d == g2[i][1] && a != g2[i][1] || d == g2[i][2] && b != g2[i][2] || d == g2[i][3] && c != g2[i][3])
                                    cw++;
                                out.println(st + " " + a + b + c + d + " " + eq + " " + cw);
                                if (eq != x[i] || cw != y[i]) {
                                    hasFs = true;
                                    break;
                                }
                            }
                            if (!hasFs) {

                                if (rA == -1) {
                                    rA = a;
                                    rB = b;
                                    rC = c;
                                    rD = d;
                                    res = 1;
                                } else res = 2;
                            }
                            if (res == 2) break;
                        }
                        if (res == 2) break;
                    }
                    if (res == 2) break;
                }
                if (res == 2) break;
            }
            if (res == 0) out.println("impossible");
            else if (res == 2) out.println("indeterminate");
            else out.println(rA + "" + rB + rC + rD);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
